/*
 * CHttpTokenAuth.h
 *
 *  Created on: 2018年2月9日
 *      Author: terry
 */

#ifndef CHTTPTOKENAUTH_H_
#define CHTTPTOKENAUTH_H_

#include "HttpTokenAuth.h"
#include "SharedPtr.h"
#include "TMap.h"
#include <atomic>

typedef std::shared_ptr< HttpUserStore >	HttpUserStorePtr;


class CHttpTokenAuth: public HttpTokenAuth
{
public:
	CHttpTokenAuth();
	virtual ~CHttpTokenAuth();

	void setUserStore(HttpUserStorePtr store);

	virtual void setExpiration(size_t seconds);

	virtual bool login(const std::string& username, const std::string& password, std::string& token);

	virtual void logout(const std::string& token);

	virtual bool check(const std::string& token, std::string& username);

	virtual void clear();

	virtual size_t size();

	virtual bool getAt(size_t idx, std::string& username, std::string& token);


	void clearExpired();

public:
	struct TokenEntry
	{
		std::string	username;
		std::string token;
		time_t	expiration;

		TokenEntry():
			expiration()
		{
		}
	};

	typedef comn::Map< std::string, TokenEntry >	TokenMap;


protected:

protected:
	HttpUserStorePtr	m_store;
	size_t	m_expiration;
	TokenMap	m_tokenMap;
	std::atomic_int	m_seq;
	time_t		m_checkTime;

};

#endif /* CHTTPTOKENAUTH_H_ */
